import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls";
import Experience from "./Experience";
export default class Camera {
  constructor() {
    this.experience = Experience.instance; //由于它是单例，所以即便在这里导入进来，也是原来的那个唯一对象。
    this.sizes = this.experience.sizes;
    this.renderer = this.experience.renderer;
    this.canvas = this.experience.canvas;
    this.scene = this.experience.scene;

    this.setInstance();
    this.setOrbitControls();


  }

  setInstance() {
   this.instance = new THREE.PerspectiveCamera(
      50, //视场，相当于眼睛看到的范围。越大看到的范围越广阔，越小看到的物体越大
      this.sizes.width / this.sizes.height,
      0.1, //低于某个阈值就不可见的距离
      5000 // 超过某个距离就不可见的阈值
    );
    this.instance.position.set(0, 0, 10);
    this.scene.add(this.instance);
  }

  setOrbitControls(){
    this.controls = new OrbitControls(this.instance, this.canvas);
    this.controls.enableDamping = true; //平滑移动
  }

  resize(){
    this.instance.aspect = this.sizes.width / this.sizes.height; //重新设置相机比例
    this.instance.updateProjectionMatrix(); //更新相机比例
  }
  update(){
    this.controls.update();
  }
}
